package com.epalmpay.controller.channel.bonus;

import com.epalmpay.commom.BaseController;
import com.epalmpay.dto.bonus.BonusRuleInDTO;
import com.epalmpay.dto.bonus.BonusRuleIndexOutDTO;
import com.epalmpay.dto.bonus.BonusSendLogListOutDTO;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.entity.BonusSendRule;
import com.epalmpay.entity.BonusVerifyRule;
import com.epalmpay.entity.Page;
import com.epalmpay.entity.Response;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.service.bonus.IBonusRuleService;
import com.epalmpay.status.Rescode;
import com.epalmpay.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by lifayi on 2017/9/22.
 */
@RestController
@RequestMapping("/merchant/bonus")
public class BonusRuleController extends BaseController{

    @Autowired
    private IBonusRuleService bonusRuleService;

    /*
    *  首页
    * */
    @RequestMapping("/index")
    public BonusRuleIndexOutDTO index(@RequestBody Long groupId) {

        return bonusRuleService.index(groupId);
    }

    @RequestMapping("/shopVerifyRuleList")
    @ResponseBody
    public BonusRuleIndexOutDTO shopVerifyRuleList(BonusRuleInDTO bonusRuleInDTO, PageDTO pageDTO){
        BonusRuleIndexOutDTO bonusRuleIndexOutDTO = new BonusRuleIndexOutDTO();
        try {
            Page page= Tools.pageEmtity(pageDTO);
            bonusRuleIndexOutDTO= bonusRuleService.shopVerifyRuleList(bonusRuleInDTO,page);
            bonusRuleIndexOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");

            bonusRuleIndexOutDTO.setPage(page);
            return bonusRuleIndexOutDTO;
        } catch (ApplicationException ae) {
            bonusRuleIndexOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        }
        catch (Exception e) {
            bonusRuleIndexOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return bonusRuleIndexOutDTO;

    }

    /*
    *  默认发放规则添加或修改
    * */
    @RequestMapping("/sendDefaultAdd")
    public BaseOutDTO sendDefaultAdd(@RequestBody BonusSendRule bonusSendRule) {
        return bonusRuleService.sendDefaultAdd(bonusSendRule);
    }

    /*
    *  默认核销规则添加或修改
    * */
    @RequestMapping("/verifyDefaultAdd")
    public BaseOutDTO verifyDefaultAdd(@RequestBody BonusVerifyRule bonusVerifyRule) {
        return bonusRuleService.verifyDefaultAdd(bonusVerifyRule);
    }


    /*
    *  行业规则删除
    * */
    @RequestMapping("/industryRuleDel")
    public BaseOutDTO industryRuleDel(@RequestBody Map<String, Object> params) {
        return bonusRuleService.industryRuleDel(params);
    }

    /*
    *  行业规则列表
    * */
    @RequestMapping("/industryRule")
    public Object industryRule(@RequestBody Map<String, Object> params) {
        return bonusRuleService.industryRule(params);
    }

    /*
    *  行业发放规则添加
    * */
    @RequestMapping("/sendIndustryAdd")
    public BaseOutDTO sendIndustryAdd(@RequestBody BonusSendRule bonusSendRule) {
        return bonusRuleService.sendIndustryAdd(bonusSendRule);
    }


    /*
    *  行业核销规则添加
    * */
    @RequestMapping("/verifyIndustryAdd")
    public BaseOutDTO verifyIndustryAdd(@RequestBody BonusVerifyRule bonusVerifyRule) {
        return bonusRuleService.verifyIndustryAdd(bonusVerifyRule);
    }

    /*
    *  获取所有行业
    * */
    @RequestMapping("/getAllIndustry")
    public List<Map> getAllIndustry() {
        return bonusRuleService.getAllIndustry();
    }

    /*
   *  获取所有门店
   * */
    @RequestMapping("/getAllShop")
    public List<Map> getAllShop(@RequestBody String groupId) {
        return bonusRuleService.getAllShop(Long.parseLong(groupId));
    }
}
